package com.bop.dehong;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.base.dehong.util.PreferencesUtils;
import com.bop.dehong.adapter.ViewPagerAdapter;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.config.Config;

/**
 * 引导页
 */
public class GuideActivity extends BaseActivity implements OnPageChangeListener {

	private ViewPager vp;
	private ViewPagerAdapter vpAdapter;
	private List<View> views;

	// 底部小点图片
	private ImageView[] dots;

	// 记录当前选中位置
	private int currentIndex;
	private View mFourView;

	private void initViews() {
		LayoutInflater inflater = LayoutInflater.from(this);

		views = new ArrayList<View>();
		// 初始化引导图片列表
		mFourView = inflater.inflate(R.layout.guide_dehong_four, null);
		registerOnClickListener(mFourView);
		mFourView.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				goHome();
				return true;
			}
		});
		views.add(inflater.inflate(R.layout.guide_dehong_one, null));
		views.add(inflater.inflate(R.layout.guide_dehong_two, null));
		views.add(inflater.inflate(R.layout.guide_dehong_three, null));
		views.add(mFourView);

		// 初始化Adapter
		vpAdapter = new ViewPagerAdapter(views);
		
		vp = (ViewPager) findViewById(R.id.viewpager);
		vp.setAdapter(vpAdapter);
		// 绑定回调
		vp.setOnPageChangeListener(this);
	}

	private void initDots() {
		LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
		dots = new ImageView[views.size()];
		// 循环取得小点图片
		for (int i = 0; i < views.size(); i++) {
			dots[i] = (ImageView) ll.getChildAt(i);
			dots[i].setEnabled(true);// 都设为灰色
		}
		currentIndex = 0;
		dots[currentIndex].setEnabled(false);// 设置为白色
	}

	private void setCurrentDot(int position) {
		if (position < 0 || position > views.size() - 1
				|| currentIndex == position) {
			return;
		}

		dots[position].setEnabled(false);
		dots[currentIndex].setEnabled(true);

		currentIndex = position;
	}
	

	private void goHome() {
		PreferencesUtils.putBoolean(this,
				Config.isNoFirstIn, true);
		// 跳转
		setResult(RESULT_OK);
		finishCurrentActivity();
	}

	// 当滑动状态改变时调用
	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	// 当当前页面被滑动时调用
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	// 当新的页面被选中时调用
	@Override
	public void onPageSelected(int arg0) {
		// 设置底部小点选中状态
		setCurrentDot(arg0);
	}

	@Override
	protected void initTop() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void initUI() {
		// 初始化页面
		initViews();
		// 初始化底部小点
		initDots();
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.guide;
	}

//	@Override
//	public void uponClick(View v) {
//		goHome();
//	}
//
//	@Override
//	public void uponItemClick(AdapterView<?> arg0, View arg1, int arg2,
//			long arg3) {
//		// TODO Auto-generated method stub
//		
//	}

	@Override
	public void onClick(View v)
	{
		goHome();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}

}
